{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from flask import Flask, request, jsonify\n",
    "import ktrain\n",
    "import numpy as np\n",
    "app = Flask(__name__)\n",
    "# 加载模型\n",
    "model = ktrain.load_model('model.pkl')\n",
    "@app.route('/predict', methods=['POST'])\n",
    "def predict():\n",
    "    # 解析请求数据\n",
    "    data = request.get_json()\n",
    "  \n",
    "    # 将数据转换为模型所需的格式\n",
    "    inputs = np.array(data['data']).reshape(-1, 1)\n",
    "  \n",
    "    # 进行预测\n",
    "    preds = model.predict(inputs)\n",
    "  \n",
    "    # 将预测结果转换为 JSON 格式并返回给客户端\n",
    "    return jsonify({'predictions': list(preds.flatten())})\n",
    "if __name__ == '__main__':\n",
    "    app.run(debug=True)"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
